
$(document).ready(function () {
    //获取角色列表
    getRoleList();

});

layui.use(['tree', 'layer', 'util','element','form'], function(){
    let $ = layui.$
        ,tree = layui.tree
        ,layer = layui.layer
        ,util = layui.util
        ,form = layui.form
        ,element = layui.element;



    tree.render({
        elem: '#roleMenuInfo'
        ,data: null
        ,id:"authority"

        ,showLine: false  //是否开启连接线
    });
    //监听select变化
    form.on('select(role)',function (data) {
        changeMenuInfoByRid(tree);

    });

    //监听提交按钮
    $("#submitAuthority").on("click",function () {
        let rId = $("#roleSelect").val();
        let checkData = tree.getChecked('authority');
        // console.log(checkData)
        let enableId = submitAuthorityChange(checkData);
        // console.log(enableId);
        enableId = $.makeArray(enableId);
        $.ajax({
            url:"/authorityEdit",
            method:"post",
            data:{"rId":rId,"enableId":'[' + enableId +']'},
            dataType:"text",
            success:function (status) {
                if (status == "1"){
                    alert("权限修改成功")
                    changeMenuInfoByRid(tree);
                }else {
                    alert("权限修改失败")
                }
            }
        });

    });


});



//清理输入框信息
function cleanerText() {
    $("input[name='roleName']").val("");
    $("input[name='description']").val("");
}

//获取角色列表
function getRoleList() {
    $.ajax( {
        url:"/getActiveRoleList",
        method:"get",
        charset: "UTF-8",
        async:false,
        success:function (res) {
            let roleSelect = $("#roleSelect");
            roleSelect.empty();
            roleSelect.append("<option value='-1'>请选择角色</option>");
            let roleList = res.data;
            $.each(roleList,function (index,value) {
                // console.log(value.id+" "+value.name+" "+value.status);
                // console.log("<option value='"+value.id +"' name='"+value.status+"'>"+value.name+"</option>");
                // console.log(roleSelect);
                if (value.id != 3){
                    roleSelect.append( "<option value='" + value.id + "'"+"name="+value.status+">" + value.name + "</option>");
                }

            });
            // console.log(roleList);
        }
    });
    // alert("...")
}




function changeMenuInfoByRid(tree) {
    let rId = $("#roleSelect").val();
    let status = $("#roleSelect").prop("name");
    // console.log(status);
    if (status == 1){
        $("#disableRole").css('display','block');
        $("#enableRole").css('display','none');
    }else {
        $("#disableRole").css('display','none');
        $("#enableRole").css('display','block');
    }
    if (rId != -1){
        // getUnableParentMenu(rId,transfer);
        $.ajax({
            url: "/roleHoldMenu",
            method: 'get',
            data: "rId="+rId,
            dataType: "json",
            success: function(data) {
                // getUnableChildMenu(rId,transfer);
                let menuList = data;
                for (let i = 0;i<data.length;i++){
                    // console.log(data[i]);
                }
                tree.render({
                    elem: '#roleMenuInfo'
                    ,data: menuList
                    ,id:"authority"
                    ,showCheckbox: true
                    ,showLine: false  //是否开启连接线
                });
            }
        });
    }else {
        tree.render({
            elem: '#roleMenuInfo'
            ,data: null
            ,id:"authority"
            ,showCheckbox: true
            ,showLine: false  //是否开启连接线
        });
    }
}

function submitAuthorityChange(data) {
    let enableId = "";
    $.each(data,function(index,item){
        if (enableId != ""){
            enableId = enableId + ","+ item.id;
        }else {
            enableId = item.id;
        }
        if (item.children != undefined){
            let enableCId = submitAuthorityChange(item.children);
            if (enableCId !=""){
                enableId = enableId + "," + enableCId;
            }
        }

    });
    return enableId;
}